//
//  currentShouYiView.m
//  SP2P
//
//  Created by IOS on 2017/7/5.
//  Copyright © 2017年 EIMS. All rights reserved.
//

#import "currentShouYiView.h"
@interface currentShouYiView ()

// 活期收益
@property (weak, nonatomic) IBOutlet UITextField *moneyTF;
@property (weak, nonatomic) IBOutlet UITextField *dayTF;
@property (weak, nonatomic) IBOutlet UIButton *resultBtn;
@property (weak, nonatomic) IBOutlet UILabel *resultLable;
@property (weak, nonatomic) IBOutlet UIButton *backGroundBtn;

@end
@implementation currentShouYiView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self = [[[NSBundle mainBundle]loadNibNamed:@"currentShouYiView" owner:self options:nil]lastObject];
        self.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.4];
        self.resultBtn.userInteractionEnabled = NO;
        [self.moneyTF addTarget:self action:@selector(limit1:) forControlEvents:UIControlEventEditingChanged];
        [self.dayTF addTarget:self action:@selector(limit2:) forControlEvents:UIControlEventEditingChanged];
        self.backGroundBtn.layer.masksToBounds = YES;
        self.backGroundBtn.layer.cornerRadius = 10;
        self.resultBtn.layer.masksToBounds = YES;
        self.resultBtn.layer.cornerRadius = 6;
        [self addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(cancelButtonClick)]];
        
    }
    return self;
}

- (void)cancelButtonClick{
    
    
    [UIView animateWithDuration:0.7 animations:^{
        [self.moneyTF resignFirstResponder];
        [self.dayTF resignFirstResponder];
        self.hidden = YES;
    }];
}
// 活期收益
- (IBAction)clickResults:(UIButton *)sender {
    NSInteger  moneCount = [self.moneyTF.text integerValue];
    NSInteger  dayCount = [self.dayTF.text integerValue];
    self.resultLable.text = [NSString stringWithFormat:@"预计可获得%.2f元收益",moneCount*0.08*dayCount/365];
    
   
    
}
//限制文本框的输入内容和文本的长度
- (void)limit1:(UITextField *)textField{
    
    //限制文本的输入长度不得大于6个字符长度
    if (self.moneyTF.text.length >= 7){
        
        //截取文本字符长度为6的内容
        self.moneyTF.text = [textField.text substringToIndex:7];
    }
    
    if (self.moneyTF.text.doubleValue > 0 && self.dayTF.text.integerValue>0) {
        [self.resultBtn setBackgroundColor:[UIColor orangeColor]];
        
        self.resultBtn.userInteractionEnabled = YES;
    }else{
        [self.resultBtn setBackgroundColor:[UIColor colorWithRed:250/255.0 green:168/255.0 blue:119/255.0 alpha:1.0]];
        self.resultBtn.userInteractionEnabled = NO;
    }
        self.resultLable.text = @"";
    
}
//限制文本框的输入内容和文本的长度
- (void)limit2:(UITextField *)textField{
    
    //限制文本的输入长度不得大于3个字符长度
    
    if (self.dayTF.text.length >= 3){
        
        //截取文本字符长度为3的内容
        self.dayTF.text = [textField.text substringToIndex:3];
    }
    if (self.moneyTF.text.doubleValue > 0 && self.dayTF.text.integerValue>0) {
        [self.resultBtn setBackgroundColor:[UIColor orangeColor]];
        self.resultBtn.userInteractionEnabled = YES;
    }else{
        [self.resultBtn setBackgroundColor:[UIColor colorWithRed:250/255.0 green:168/255.0 blue:119/255.0 alpha:1.0]];
        self.resultBtn.userInteractionEnabled = NO;
    }
        self.resultLable.text = @"";
    
    
}


@end
